package com.example.demo6.controller;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/20 21:30
 */
import com.example.demo6.entity.Goods;
import com.example.demo6.entity.Inventory;
import com.example.demo6.entity.Suppliers;
import com.example.demo6.service.GoodsService;
import com.example.demo6.service.InventoryService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.http.HttpServerConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Controller
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    InventoryService inventoryService;
    @Autowired
    HttpServletRequest request;
    @RequestMapping("/inventoryList")
    public String inventoryList(Model model){
        Object username=request.getSession().getAttribute("username");
        System.out.println(username);
        if (username==null){
            return "redirect:/";
        }else{
//        1 用service方法查询列表
            List<Inventory> inventoryList = inventoryService.findAll();
//        Object username=request.getSession().getAttribute("username");

            System.out.println(inventoryList);
//        将查询结果用model绑定
            model.addAttribute("data",inventoryList);
            model.addAttribute("username",username);

//        return视图
            return "inventory/inventoryList";}
    }
    @RequestMapping("/inventoryAdd")
    public String inventoryAdd(Model model){
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/";
        }else{
//        Object username=request.getSession().getAttribute("username");
            model.addAttribute("username",username);
            return "inventoryAdd";}
    }
    @RequestMapping("/inventoryAddProcess")
    public String inventoryAddProcess(@RequestParam String name,@RequestParam String quantity){
//1 组织成实体
        Inventory inventory = new Inventory();
        inventory.setName(name);
//        goods.setPrice(Double.valueOf(price));
        inventory.setQuantity(quantity);
        if (inventoryService.add(inventory)==1){
            return "redirect:/inventory/inventoryList";
        }else {
            return "redirect:/inventory/inventoryList";

        }
    }
    @RequestMapping("/inventoryUpdate")
    public String inventoryUpdate(@RequestParam String id,Model model){
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/";
        }else{
//        首先根据id查询到该条记录信息
            Inventory inventory = new Inventory();
            inventory.setId(Integer.valueOf(id));
            Inventory inventorydb = inventoryService.findone(inventory);
//        再将数据绑定到视图
            model.addAttribute("data",inventorydb);
//        Object username=request.getSession().getAttribute("username");
            model.addAttribute("username",username);

            return "inventoryUpdate";}
    }

}
